﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Swoosh.Contracts.MediaPlayerLib
{
    [Serializable]
    public struct FromToPair<F, T>
    {
        public F From
        { get; set; }

        public T To
        { get; set; }

        public FromToPair(F f, T t) : this() { From = f; To = t; }
    } 

    [Serializable]
    public class Storage
    {
        public string Name { get; set; }

        [XmlArrayItem("ConverterItem")]
        public List<StorageConverter> Converters { get; set; }

        public Storage()
        {
            Name = "";
            Converters = new List<StorageConverter>();
        }

        public string Convert(string dir, string target)
        {
            StorageConverter converter = Converters.Where(c => c.DestinationStorage == target).FirstOrDefault();
            if (converter != null)
            {
                return converter.Convert(dir);
            }
            return dir;
        }   
    }

    [Serializable]
    public class StorageConverter
    {
        private string _DestinationStorage;
        public string DestinationStorage
        {
            get { return _DestinationStorage; }
            set { _DestinationStorage = value; }
        }

        private List<FromToPair<string, string>> _PrefixTransformation;
        [XmlArrayItem("PrefixTransformationItem")]
        public List<FromToPair<string, string>> PrefixTransformation
        {
            get { return _PrefixTransformation; }
            set { _PrefixTransformation = value; }
        }

        public StorageConverter()
        {
            _DestinationStorage = "";
            _PrefixTransformation = new List<FromToPair<string, string>>();
        }

        public string Convert(string dir)
        {
            foreach (FromToPair<string, string> item in _PrefixTransformation)
            {
                if (dir.ToLower().StartsWith(item.From))
                {
                    return (item.To + dir.Substring(item.From.Length)).ToLower();
                }
            }
            return dir;
        }        
    }
}
